package com.wky.org.service.Impl;

import com.wky.basic.Result.PageResult;
import com.wky.basic.service.impl.BaseServiceImpl;
import com.wky.org.mapper.DepartmentMapper;
import com.wky.org.mapper.EmployeeMapper;
import com.wky.org.pojo.Department;
import com.wky.org.pojo.Employee;
import com.wky.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServicerImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;





    /**
     *  分页查询部门
     * @param department
     * @return
     */
    @Override
    public PageResult<Department> findByPage(Department department) {
        //分页数据
        List<Department> departments = departmentMapper.findByPage(department);
        for (Department dept : departments) {
            //部门领导
            if (dept.getManagerId() != null){
                Employee employee = employeeMapper.findById(dept.getManagerId());
                dept.setManager(employee);
            }
            //上级部门
            if (dept.getParentId() != null){
                Department parent = departmentMapper.findById(dept.getParentId());
                dept.setParent(parent);
            }
        }

        //总条数
        Integer total = departmentMapper.findTotal(department);

        return new PageResult<Department>(total,departments);
    }


    /**
     *  添加部门
     * @param department
     */
    @Override
    public void add(Department department) {
        //添加的部门为顶级，返回自增主键（先添加，后更新，根据主键id->更新dirPath）
        departmentMapper.add(department);
        //更新部门
        update(department);

    }


    /**
     *  修改部门
     * @param department
     */
    @Override
    public void update(Department department) {

        //修改部门若为顶级
        if (department.getParentId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //修改部门为子部门(若为本部门)
            if (department.getParentId()==department.getId()){
                Department parent = departmentMapper.findById(department.getParentId());
                department.setDirPath(parent.getDirPath());
                department.setParentId(parent.getParentId());
            }else {
                //修改部门为子部门(非本部门)
                Department parent = departmentMapper.findById(department.getParentId());
                department.setDirPath(parent.getDirPath()+"/"+department.getId());
            }

        }

        //更新部门
        departmentMapper.update(department);
    }



    /**
     *  查询部门树
     * @return
     */
    @Override
    public List<Department> tree() {
        // 1. 查询所有部门
        List<Department> all = departmentMapper.findAll();

        // 2. 构建id->部门 映射
//        Map<Long, Department> map = all.stream().collect(Collectors.toMap(Department::getId, department -> department));
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(), department);
        }

        // 3. 构建树
        List<Department> tree = new ArrayList<>();
        for (Department department : all) {
            if (department.getParentId() == null) {
                tree.add(department);
            } else {
                Department parent = map.get(department.getParentId());
                parent.getChildren().add(department);
            }
        }

        return tree;

    }
}
